package com.zrrd.blog.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.blog.api.entities.UserInfoREQ;
import com.zrrd.blog.article.service.IArticleService;
import com.zrrd.blog.api.entities.Article;
import com.zrrd.blog.article.mapper.ArticleMapper;
import com.zrrd.blog.article.req.ArticleListREQ;
import com.zrrd.blog.article.req.ArticleREQ;
import com.zrrd.blog.article.req.ArticleUserREQ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.blog.util.base.Result;
import com.zrrd.blog.util.enums.ArticleStatusEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 文章信息表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-08-06
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Override
    public Result queryPage(ArticleREQ req) {
        QueryWrapper<Article> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(req.getTitle())){
            wrapper.like("title",req.getTitle());
        }
        if(req.getStatus()!=null){
            wrapper.eq("status",req.getStatus());
        }
        wrapper.orderByDesc("update_date");
        return Result.ok(baseMapper.selectPage(req.getPage(),wrapper));
    }

    @Override
    public Result findArticleAndLabelById(String id) {
        return Result.ok(baseMapper.findArticleAndLabelById(id));
    }

    @Override
    public Result updateOrSave(Article article) {
        //如果id不为空，则为删除操作
        if(StringUtils.isNotEmpty(article.getId())){
            //更新，先删除中间表数据
            //因为在修改文章的时候可能会修改到文章的Label，所以要先删除，再重新插入
            baseMapper.deleteArticleLable(article.getId());
            //将更新时间设置为当前时间
            article.setUpdateDate(new Date());
        }else{
            //新增文章，状态为未审核
            article.setStatus(ArticleStatusEnum.NOTCHECK.getCode());
        }
        //如果文章是非公开，则直接审核通过
        if(article.getIspublic()==0){
            article.setStatus(ArticleStatusEnum.SUCCESS.getCode());
        }
        super.saveOrUpdate(article);
        //新增标签到删除表中
        if(CollectionUtils.isNotEmpty(article.getLabelIds())){
            baseMapper.saveArticleLable(article.getId(),article.getLabelIds());
        }
        return Result.ok(article.getId());
    }

    @Override
    public Result updateStatus(String id, int code) {
        Article article=baseMapper.selectById(id);
        article.setStatus(code);
        article.setUpdateDate(new Date());
        baseMapper.updateById(article);
        return Result.ok();
    }

    @Override
    public Result findListByUsers(ArticleUserREQ req) {
        if(StringUtils.isEmpty(req.getUserId())){
            return Result.error("无效用户信息");
        }
        QueryWrapper<Article> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",req.getUserId());
        if(req.getIsPublic()!=null){
            wrapper.eq("ispublic",req.getIsPublic());
        }
        return Result.ok(baseMapper.selectPage(req.getPage(),wrapper));
    }

    @Override
    public Result updateThumbup(String id, int count) {
        if(count!=-1 && count!=1){
            return Result.error("无效操作");
        }
        if(StringUtils.isEmpty(id)){
            return Result.error("无效操作");
        }
        Article article=baseMapper.selectById(id);
        if(article==null){
            return Result.error("文章不存在");
        }
        article.setThumhup(article.getThumhup()+count);
        baseMapper.updateById(article);
       return Result.ok();
    }

    @Override
    public Result updateViewCount(String id) {
        if(StringUtils.isEmpty(id)){
            return Result.error("无效操作");
        }
        Article article=baseMapper.selectById(id);
        if(article==null){
            return Result.error("文章不存在");
        }
        article.setViewCount(article.getViewCount()+1);
        baseMapper.updateById(article);
        return Result.ok();
    }

    @Override
    public Result findListByLabelIdOrCategoryId(ArticleListREQ req) {
        return Result.ok(baseMapper.findListByLabelIdOrCategoryId(req.getPage(),req));
    }

    @Override
    public Result getArticleTotal() {
        //select * from zrrd_article
        QueryWrapper<Article> wrapper=new QueryWrapper<>();
        //where status=2
        wrapper.eq("status",ArticleStatusEnum.SUCCESS.getCode());
        //and ispublic=1
        wrapper.eq("ispublic",1);
        //select count(*)
        Integer total =baseMapper.selectCount(wrapper);
        return Result.ok(total);
    }

    @Override
    public Result selectCategoryTotal() {
        List<Map<String,Object>> maps=baseMapper.selectCategoryTotal();
        System.out.println(maps);
        //将分类的名字提取到单独的集合中
        List<Object> nameList =new ArrayList<>();
        for(Map<String,Object> map:maps){
            nameList.add(map.get("name"));
        }
        System.out.println(nameList);
        Map<String,Object> data=new HashMap<>();
        data.put("nameAndValue",maps);
        data.put("nameList",nameList);
        return Result.ok(data);

    }

    @Override
    public boolean updateUserInfo(UserInfoREQ req) {
        System.out.println(req);
        return baseMapper.updateUserInfo(req);
    }


}
